<?php
namespace MobileHome\Controller;
use Think\Page;

class NewsController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function indexAction()
    {
        $cat_id = I('cat_id', 0, 'intval');
        $cat_info = M('NewsCategory')->find($cat_id);
        $News = M('News');
        $condition = array(
            'cat_id' => $cat_id,
        );
        $count = $News->where($condition)->count();
        $Page = new Page($count, 50);
        $Page->setConfig('header', '');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '');
        $show = $Page->show();
        $list = $News->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $rec_news = array();
        if ($list) {
            $rec_news = $list[0];
            unset($list[0]);
        }
        $this->assign('rec_news', $rec_news);
        $this->assign('page', $show);
        $this->assign('list', $list);
        $global_config = array(
            'title' => $cat_info['name'] . '-' . C('SITE_NAME'),
            'keywords' => $cat_info['name'],
            'description' => $cat_info['name'],
        );
        $cat_list = M('NewsCategory')->order('sort asc')->select();
        $this->assign('cat_list', $cat_list);
        $this->assign('cat_id', $cat_id);
        $this->assign('global_config', $global_config);
        $this->display();
    }

    public function detailAction()
    {
        $cat_id = I('get.cat_id', 0);
        $id = I('get.id', 0);
        $news_info = M('News')->find($id);
        $global_config = array(
            'title' => $news_info['title'] . '-' . C('SITE_NAME'),
            'keywords' => $news_info['title'],
            'description' => $news_info['title'],
        );
        $cat_list = M('NewsCategory')->order('sort asc')->select();
        $this->assign('cat_list', $cat_list);
        $this->assign('cat_id', $cat_id);
        $this->assign('news_info', $news_info);
        $this->assign('global_config', $global_config);
        $this->display();
    }
}